package com.wokeMy.view.Lottery;


import android.app.Dialog;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wokeMy.view.adpter.HorLvAdapter;
import com.wokeMy.view.adpter.OpenHorLvAdapter;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.TicketDteal;
import com.wokeMy.view.util.Util;
import com.wokeMy.view.view.HorizontalListView;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class DetailBetActivity extends BaseActivity {
    HorizontalListView betNum_horlv,openNum_horlv;
    private TextView tickettitle;
    private ImageView ticketback;
    private RelativeLayout bdetaiiltop;
    private ImageView detailimage;
    private LinearLayout lotterysxleft;
    private TextView detailkind;
    private TextView detailissue;
    private LinearLayout aaa;
    private TextView lrcstate;
    private LinearLayout lotterysxright;
    private TextView detailmoney;
    private TextView betdetailtv;
    private TextView detailzjtitle;
    private TextView detailzjmoney;
    private TextView detailtime;
    private TextView detailinfor;
    TicketDteal ticket;
    ArrayList<String> ball,openBall;
    String size,openSize;
    HorLvAdapter horLvAdapter;
    OpenHorLvAdapter openAdapter;
    Dialog mydialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_detail_bet);

    }

    @Override
    public void initView() {
       betNum_horlv= (HorizontalListView)findViewById(R.id.betNum_horlv);
       openNum_horlv= (HorizontalListView)findViewById(R.id.openNum_horlv);
        this.detailinfor = (TextView) findViewById(R.id.detail_infor);
        this.detailtime = (TextView) findViewById(R.id.detail_time);
        this.detailzjmoney = (TextView) findViewById(R.id.detail_zjmoney);
        this.detailzjtitle = (TextView) findViewById(R.id.detail_zjtitle);
        this.betdetailtv = (TextView) findViewById(R.id.betdetail_tv);
        this.detailmoney = (TextView) findViewById(R.id.detail_money);
        this.lotterysxright = (LinearLayout) findViewById(R.id.lotterysx_right);
        this.lrcstate = (TextView) findViewById(R.id.lrc_state);
        this.aaa = (LinearLayout) findViewById(R.id.aaa);
        this.detailissue = (TextView) findViewById(R.id.detail_issue);
        this.detailkind = (TextView) findViewById(R.id.detail_kind);
        this.lotterysxleft = (LinearLayout) findViewById(R.id.lotterysx_left);
        this.detailimage = (ImageView) findViewById(R.id.detail_image);
        this.bdetaiiltop = (RelativeLayout) findViewById(R.id.bdetaiil_top);
        this.ticketback = (ImageView) findViewById(R.id.detail_back);
        this.tickettitle = (TextView) findViewById(R.id.detail_title);
    }

    @Override
    public void initData() {
        String id=getIntent().getStringExtra("id");
        ball=new ArrayList<>();
        openBall=new ArrayList<>();
        mydialog=Util.createLoadingDialog(DetailBetActivity.this);
        cpDetai(id);

    }

    @Override
    public void initActivity() {
        ticketback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    // 彩票详情
    public void  cpDetai(String id){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("id",id);//玩法代码 118//双色球 106 大乐透
        Log.e("彩票详情url", Constant.CPDETAIL_URL);
        Log.e("彩票详情params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CPDETAIL_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    Util.closeDialog(mydialog);
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("彩票详情result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.get("flag").equals("success")) {
                            Gson gson = new Gson();
                            String data = jsonObject.getJSONObject("data").getString("data");
                            ticket = gson.fromJson(data, TicketDteal.class);
                            Log.e("ticket",""+ticket.toString());
                            initview();
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(mydialog);
            }
        });
    }

    public void initview(){
        // 118双色球 106大乐透
        int rid;
        switch (ticket.getLotteryid()) {
            case "118":
                rid=R.mipmap.double_ball;
                openSize="6";
                break;
            case "106":
                rid=R.mipmap.happy_ball;
                openSize="5";
                break;
            case "123":
                rid=R.mipmap.night_ball;
                openSize="5";
                break;
            default:
                rid=R.mipmap.double_ball;
                openSize="6";
                break;
        }
        detailimage.setImageResource(rid);
        detailkind.setText(ticket.getTitle());
        detailissue.setText(ticket.getIssue());
        String status,title,content;
        //出票状态 0不可出票 1可出票状态 2出票成功 3出票失败(允许再出票）4出票中 5出票中（体彩中心）6出票失败（不允许出票） 99 为空
        if (ticket.getTicketstatus().equals("99")||ticket.getTicketstatus().equals("4")||ticket.getTicketstatus().equals("5")) {
            status="出票中";
            title = "出票中，感谢您助力";
            content = "公益事业！";
        }else {
            if (ticket.getTicketstatus().equals("2")||ticket.getTicketstatus().equals("1")) {
                //2出票成功
                switch (ticket.getZjstatus()) {
                    case "0":
                        status = "等待开奖";
                        title = "等待开奖,感谢您助力";
                        content = "公益事业！";
                        break;
                    case "1":
                        status = "未中奖";
                        title = "未中奖,感谢您助力";
                        content = "公益事业！";
                        break;
                    case "2":
                        status = "恭喜中奖";
                        lrcstate.setTextColor(Color.RED);
                        title = "中奖金额";
                        content = Integer.valueOf(ticket.getPrebonusvalue()) / 100 + "元";
                        break;
                    case "5":
                        status = "等待开奖";
                        title = "等待开奖,感谢您助力";
                        content = "公益事业！";
                        break;
                    default:
                        status = "投注成功";
                        title = "感谢您助力";
                        content = "公益事业！";
                        break;
                }
            }else {
                status = "投注成功";
                title = "感谢您助力";
                content = "公益事业！";
            }
        }
        lrcstate.setText(status);
        detailzjtitle.setText(title);
        detailzjmoney.setText(content);
        detailissue.setText("第"+ticket.getIssue()+"期");
        detailissue.setText("第"+ticket.getIssue()+"期");
        detailmoney.setText(ticket.getLotteryvalue()+"元");
        detailtime.setText(Util.getStrTi(ticket.getCreatetime()));
        if (ticket.getLotteryid().equals("123")){
            String tip;
            switch (ticket.getChildtype()) {
                case "1":
                    tip="和值";
                    break;
                case "2":
                    tip="三同号";
                    break;
                case "3":
                    tip="三不同号";
                    break;
                case "4":
                    tip="三连号通选";
                    break;
                case "5":
                    tip="二同号";
                    break;
                default:
                    tip="和值";
                    break;
            }
            detailinfor.setText(tip+" "+ticket.getLotterynumber()+"注 "+ticket.getAppnumbers()+"倍");
        }else {
            detailinfor.setText(ticket.getLotterynumber()+"注 "+ticket.getAppnumbers()+"倍");
        }
        ball = Util.dealtIssudetial(ticket.getLotterycode());
        if (ticket.getLotteryid().equals("123")){
            //赢快3
            size=ball.size()+"";
            openSize=ball.size()+"";
        }else {
            size=ticket.getLotterycode().indexOf("|")/2+"";
        }
        horLvAdapter = new HorLvAdapter(DetailBetActivity.this,ball,size,ticket.getLotteryid());
        betNum_horlv.setAdapter(horLvAdapter);
        horLvAdapter.notifyDataSetChanged();
        if (!ticket.getBonuscode().equals("")){
            openBall = Util.dealtIssueGg(ticket.getBonuscode());
            if (ticket.getLotteryid().equals("123")){
                openSize=openBall.size()+"";
            }
            int sum=0;
            for (int i = 0; i < openBall.size(); i++) {
                sum+=Integer.valueOf(openBall.get(i));
            }
            openAdapter = new OpenHorLvAdapter(DetailBetActivity.this,openBall,openSize,"和值为: "+sum,ticket.getChildtype());
            openNum_horlv.setAdapter(openAdapter);
            openAdapter.notifyDataSetChanged();
        }
    }
}
